<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends CI_Controller {

    //Init $layout_data
    private static $layout_data = array();

    //Init $category_data
    private static $category_data = array();

    /**
     * construct function
     */
    public function __construct()
    {
        parent::__construct();

        //Assign to layouts
        $this->layout_data['pageTitle'] = 'Category - My site - Backend - Mr.Alex';
        $this->layout_data['navTab'] = 'category';

        //Init category model
        $this->load->model('category_model', 'categoryModel');
    }

    /**
     * Index function
     */
    public function index()
    {
        //Get list category
        $this->category_data['categoryList'] = $this->categoryModel->getCategoryList();


        //Set layouts content data
        $this->layout_data['content'] = $this->load->view('category/index', $this->category_data, TRUE);

        //Render layouts
        $this->load->view('layouts/main', $this->layout_data);
    }

    /**
     * Add category function
     */
    public function add()
    {
        if($this->input->post('submit'))
        {
            //Check title NOT null
            if($this->input->post('Name'))
            {
                //Get params
                $params = array(
                    'ParentID'  =>  $this->input->post('ParentID'),
                    'Type'  =>  $this->input->post('Type'),
                    'Name'  =>  $this->input->post('Name'),
                    'Description'  =>  $this->input->post('Description'),
                    'Status'  =>  $this->input->post('Status'),
                    'CreateDate'  =>  time(),
                );

                //insert to DB
                $result = $this->categoryModel->insertCategory($params);

                //Check result
                if($result)
                {
                    $this->category_data['categorySuccess'] = 'Category '.$this->input->post('Name').' đã được thêm thành công';
                }
                else
                {
                    $this->category_data['categoryError'] = 'Có lỗi xảy ra, vui lòng thử lại';
                }
            }
            else
            {
                $this->category_data['categoryError'] = 'Vui lòng điền tên category';
            }
        }

        //Get category
        $this->category_data['categoryRoot'] = $this->categoryModel->getCategoryRoot();

        //Set layouts content data
        $this->layout_data['content'] = $this->load->view('category/add', $this->category_data, TRUE);

        //Render layouts
        $this->load->view('layouts/main', $this->layout_data);
    }

    /**
     * Update category function
     */
    public function update()
    {
        if($this->input->post('submit'))
        {
            //Check title NOT null
            if($this->input->post('Name'))
            {
                //Get params
                $params = array(
                    'ParentID'  =>  $this->input->post('ParentID'),
                    'Type'  =>  $this->input->post('Type'),
                    'Name'  =>  $this->input->post('Name'),
                    'Description'  =>  $this->input->post('Description'),
                    'Status'  =>  $this->input->post('Status')
                );

                //Get categoryId
                $categoryId = $this->uri->segment(4);

                //Check categoryId
                if($categoryId)
                {
                    //Update to DB
                    $this->categoryModel->editCategory($categoryId, $params);

                    //Redirect index
                    redirect('category');
                }
                else
                {
                    $this->category_data['categoryError'] = 'Không tìm thấy category';
                }
            }
            else
            {
                $this->category_data['categoryError'] = 'Vui lòng điền tên category';
            }
        }

        //Get list category root
        $this->category_data['categoryRoot'] = $this->categoryModel->getCategoryRoot();

        //Get categoryId
        $categoryId = $this->uri->segment(4);

        //Check categoryId
        if($categoryId)
        {
            //Get categoryDetail
            $this->category_data['categoryDetail'] = $this->categoryModel->getCategoryDetail($categoryId);
        }

        //Set layouts content data
        $this->layout_data['content'] = $this->load->view('category/update', $this->category_data, TRUE);

        //Render layouts
        $this->load->view('layouts/main', $this->layout_data);
    }

    /**
     * Remove category function
     */
    public function remove()
    {
        //Get categoryId
        $categoryId = $this->uri->segment(4);

        //Check categoryId
        if($categoryId)
        {
            //Remove category
            $this->categoryModel->deleteCategory($categoryId);

            redirect('category');
        }
    }

}

/* End of file Home.php */
/* Location: ./application/controllers/welcome.php */